/*
    This file is part of p4f
    Copyright (C) 2009  Sebastian Fernandez

    p4f is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as 
    published by the Free Software Foundation, either version 3 of the 
    License, or (at your option) any later version.

    p4f is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
*/

#ifndef _SOCKET_H_
#define _SOCKET_H_


#include <native.h>
#include <string>

class Net{
public:
	Net();
	~Net();
private:
	bool init;
#ifdef _WIN32
	WSADATA wsaData;
	WORD wVersionRequested;
#endif
};

class Socket {
public:

	//Initialize, destruct
	Socket();
	Socket(SOCKET s);
	Socket(Socket &s);
	Socket(int domain, int type, int protocol);
	~Socket();
	
	//Connection
	bool bind(int port);
	bool listen();
	bool connect(std::string host, int port);
	bool accept(Socket &new_socket);
	Socket *accept();
		
	//Comunication with string
	int send(std::string data);
	int recv(std::string &data, int length);
	int sendto(std::string host, int port, std::string data);
	int recvfrom(std::string &data, int length);

	//Comunication with data(char *)
	int send(const char *data, int length);
	int recv(char *data, int length);
	int sendto(const char *host, int port,const char *data, int length);
	int recvfrom(char *data, int length);
	
	
	int max_connections;

protected:
	struct in_addr addr_from_host(const char *host);
	SOCKET ssock;
	struct sockaddr_in saddr;

	//For use when needing 2 structs
	struct sockaddr_in soaddr;

	int remote_port;
	std::string remote_addr;
	std::string error;
};


class TcpSocket : public Socket{
public:
	TcpSocket();
	
	bool listen(int port);
};

class UdpSocket : public Socket{
public:
	UdpSocket();
	//~UdpSocket();

	bool listen(int port);
	int send(const char *data, int length);
	int send(std::string data);
	bool setPeer(std::string host, int port);
	int recv(char *data, int length);
	int recv(std::string &data, int length);
	
	const char *get_remote_host();
	int get_remote_port();
};

#endif /* _SOCKET_H_ */
